/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.TypeFormulaire;
import static Controleur.TypeFormulaire.AFFICHAGE;
import static Controleur.TypeFormulaire.CREATION;
import Controleur.VeloFormControleur;
import Modele.Velo;
import javax.swing.JOptionPane;

/**
 *
 * @author 20cent
 */
public class VeloForm extends javax.swing.JDialog {

    private TypeFormulaire mode;
    private VeloFormControleur controleur;
    private Velo velo;

    /**
     * Creates new form VeloForm
     */
    public VeloForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode) {
        super(parent, modal);
        initComponents();

        //initialisation du controleur
        controleur = new VeloFormControleur(this);

        this.mode = mode;
        // on active les controles en fonction du mode
        appliquerMode();
    }

    public VeloForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode, Velo velo) {
        this(parent, modal, mode);
        this.velo = velo;
        if (mode != TypeFormulaire.CREATION) {
            tf_no_serie.setText(String.valueOf(velo.getmSerial()));
            tf_date.setText(velo.getMDateEntree().toString());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_titre = new javax.swing.JLabel();
        label_no_serie = new javax.swing.JLabel();
        tf_no_serie = new javax.swing.JTextField();
        panel_boutons = new javax.swing.JPanel();
        btn_annuler = new javax.swing.JButton();
        btn_enregistrer = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tf_date = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        label_titre.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        label_titre.setText("Vélo");

        label_no_serie.setLabelFor(tf_no_serie);
        label_no_serie.setText("Numéro de série");

        tf_no_serie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_no_serieActionPerformed(evt);
            }
        });

        btn_annuler.setText("Annuler");
        btn_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_annulerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_annuler);

        btn_enregistrer.setText("Enregistrer");
        btn_enregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_enregistrerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_enregistrer);

        jLabel1.setText("Date de mise en circulation");

        tf_date.setEditable(false);
        tf_date.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_dateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel_boutons, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(tf_date))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_no_serie)
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_titre, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_no_serie, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_titre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_no_serie)
                    .addComponent(tf_no_serie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(panel_boutons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_no_serieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_no_serieActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_no_serieActionPerformed

    private void btn_annulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_annulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_annulerActionPerformed

    private void btn_enregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_enregistrerActionPerformed

        if (tf_no_serie.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Champ manquant", "Erreur", JOptionPane.WARNING_MESSAGE);
        } else {
            btn_enregistrer.setEnabled(false);
            try {
                int serial = Integer.parseInt(tf_no_serie.getText());
                velo.setmSerial(serial);
                controleur.enregistrer(velo);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this,
                            "Un nombre est attendu", "Erreur", JOptionPane.WARNING_MESSAGE);
            }

        }

    }//GEN-LAST:event_btn_enregistrerActionPerformed

    private void tf_dateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_dateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_dateActionPerformed

    private void appliquerMode() {
        switch (mode) {
            case AFFICHAGE:
                activerChamps(false);
                //activation des boutons
                btn_annuler.setVisible(false);
                btn_enregistrer.setVisible(false);
                break;
            case CREATION:
                activerChamps(true);
                //activation des boutons
                btn_annuler.setVisible(true);
                btn_enregistrer.setVisible(true);
                break;
            default:
                break;
        }
    }

    private void activerChamps(boolean bool) {
        tf_no_serie.setEditable(bool);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_annuler;
    private javax.swing.JButton btn_enregistrer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel label_no_serie;
    private javax.swing.JLabel label_titre;
    private javax.swing.JPanel panel_boutons;
    private javax.swing.JTextField tf_date;
    private javax.swing.JTextField tf_no_serie;
    // End of variables declaration//GEN-END:variables
}
